package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MenuDao;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author: 颛诚诚
 * @Date： 2020/9/25   11:42
 */
@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService{
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;
    @Autowired
    private MenuDao menuDao;
    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
            PageHelper.startPage(currentPage, pageSize);
            Page<Role> page = roleDao.selectByCondition(queryString);
            long total = page.getTotal();
            List<Role> result = page.getResult();
            return new PageResult(total,result);
    }

    @Override
    public void add(Integer[] permissionIds, Integer[] menuIds, Role role) {
        roleDao.add(role);
        Integer id = role.getId();
        this.setRoleAndMenu(id,menuIds);
        this.setRoleAndPermissionIds(id,permissionIds);
    }

    @Override
    public Role findById(Integer id) {
        return roleDao.findById(id);
    }

    @Override
    public void update(Integer[] permissionIds, Integer[] menuIds, Role role) {
        Integer roleId = role.getId();
        roleDao.deleteRoleAndPermission(roleId);
        roleDao.deleteRoleAndMenu(roleId);
        roleDao.update(role);
        this.setRoleAndPermissionIds(roleId, permissionIds);
        this.setRoleAndMenu(roleId, menuIds);
    }

    @Override
    public Result delete(Integer id) {
        try {
            Integer userId = roleDao.findUserAndRole(id);
            if (userId != null && userId != 0){
                return new Result(false, "该角色已被用户使用,不允许删除");
            }else{
                roleDao.deleteRoleAndPermission(id);
                roleDao.deleteRoleAndMenu(id);
                roleDao.delete(id);
                return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
        }

    }

    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }

    @Override
    public List<Integer> findRoleIdsByUserId(Integer userId) {
        return roleDao.findRoleIdsByUserId(userId);
    }

    public void setRoleAndPermissionIds(Integer id, Integer[] permissionIds) {
        if (permissionIds != null && permissionIds.length > 0){
            List<Integer> list = new ArrayList<>();
            for (Integer permissionId : permissionIds) {
                list.add(permissionId);
            }


            roleDao.setRoleAndPermissionIds(id,list);
        }
    }

    public void setRoleAndMenu(Integer id, Integer[] menuIds) {
        if (menuIds != null && menuIds.length > 0){
            List<Integer> list = new ArrayList<>();
            for (Integer menuId : menuIds) {
                list.add(menuId);
            }
            roleDao.setRoleAndMenu(id,list);
        }
    }
}
